#include "service.h"
#include "server.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>         
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "package.h"
#include <unistd.h>
#include "../c/call_py.h"
#include <fcntl.h>
#include <time.h>
#include "data.h"



void lpr(struct service *args)
{
    int fd;
    int i = 0,len,num = 0;
    time_t t;
    char name[64];
    struct tm *st;
    len = atoi(args->p.field1);
    t = atol(args->p.field2);
    st = localtime(&t);
    sprintf(name,"../img/%d%d%d%d%d%d.bmp",st->tm_year+1900,st->tm_mon+1,st->tm_mday,st->tm_hour,st->tm_min,st->tm_sec);
    if((fd = open(name,O_RDWR | O_CREAT,0777)) < 0){
        perror("open");
        return;
    }
    send_package(args->ckt,package(&args->p));
    while((num = read(args->ckt,args->buffer,args->size)) > 0){
        i += num;
        write(fd,args->buffer,num);
        if(i == len)
            break;
    }

    python_call(&args->pobj,name);

    strcpy(args->p.field1,args->pobj.car_id);

    send_package(args->ckt,package(&args->p));

    printf("\n*********车牌识别结束**********\n%s\n",args->pobj.car_id);
}

void in_parking(struct service *args)
{
    int ret;
    struct data d;
    sprintf(args->buffer,"INSERT INTO garage VALUES (\"%s\",%s,0,1);",args->p.field1,args->p.field2);
    d.sql = args->buffer;
    d.ppDb = args->ppDb;
    if((ret = data_query(&d)) < 0){
        strcpy(args->p.field3,d.errMsg);
    }else{
        strcpy(args->p.field3,"ok\0");
    }
    data_clear(&d);

    send_package(args->ckt,package(&args->p));
}


static int get_cost(int t1,int t2)
{
    int t = (t2 - t1) / 60;
    if(t < 30)
        return 0;
    //printf("%d\n",t);
    return (t/60+1) * 20;
}

void out_parking(struct service *args)
{
    int ret;
    struct data d;
    struct value v;
    int cost;
    sprintf(args->buffer,"SELECT time1 FROM garage WHERE id = \"%s\" AND state = 1;",args->p.field1);
    d.sql = args->buffer;
    d.ppDb = args->ppDb;
    //printf("%s\n",args->buffer);
    if((ret = data_query(&d)) < 0){
        strcpy(args->p.field3,d.errMsg);
    }else{
        if(data_get(&d,&v)){
            cost = get_cost(atoi(v.data[0]),atoi(args->p.field2));
            sprintf(args->p.field3,"ok%d",cost);
            sprintf(args->p.field2,"%f",(atoi(args->p.field2) - atoi(v.data[0])) / 60.0);
            //printf("%s\n%s\n",args->p.field3,args->p.field2);
            sprintf(args->buffer,"UPDATE garage SET time2 = %s,state=0 WHERE id = \"%s\" AND state = 1;",args->p.field2,args->p.field1);
            data_query(&d);
        }else{
            strcpy(args->p.field3,"NO DATA\n");
        }
    }
    data_clear(&d);

    send_package(args->ckt,package(&args->p));
}


void is_in(struct service *args)
{
    int ret;
    struct data d;

    sprintf(args->buffer,"SELECT * FROM garage WHERE id = \"%s\" AND state = 1;",args->p.field1);
    d.sql = args->buffer;
    d.ppDb = args->ppDb;
    //printf("%s\n",args->buffer);
    if((ret = data_query(&d)) < 0){
        strcpy(args->p.field3,d.errMsg);
    }else{
        if(data_empty(&d)){
            strcpy(args->p.field3,"ok\0");
        }else{
            strcpy(args->p.field3,"parking\0");
        }
    }
    data_clear(&d);

    send_package(args->ckt,package(&args->p));
}


